# -*- coding: utf-8 -*-
import scrapy
from scrapy_yamaxun.items import ScrapyYamaxunItem

class YamaxunSpider(scrapy.Spider):
    name = 'yamaxun'
    # allowed_domains = ['www.amazon.cn/gp/bestsellers/gift-cards/ref=zg_bs_pg_1']
    start_urls = ['https://www.amazon.cn/gp/bestsellers/gift-cards/ref=zg_bs_pg_1']
    # https: // www.amazon.cn / gp / bestsellers / gift - cards / ref = zg_bs_pg_2 / 457 - 1229286 - 5416907?ie = UTF8 & pg = 2

    def parse(self, response):
        allli = response.xpath("//ol[@class='a-ordered-list a-vertical']/li[@class='zg-item-immersion']")
        # print(str(len(allli))+"hhhhh")
        for li in allli:
            item = ScrapyYamaxunItem()
            item['index'] = li.xpath("./span[@class='a-list-item']//span[@class='zg-badge-text']/text()").extract()[0]
            item['name'] = li.xpath("./span[@class='a-list-item']/div/span/a/div/text()").extract()[0]
            try:
                item['fav_num'] = li.xpath("./span[@class='a-list-item']//a[@class='a-size-small a-link-normal']/text()").extract()[0]
            except:
                item['fav_num'] = "没有收藏数"
            try:
                item['price'] = li.xpath("./span[@class='a-list-item']//span[@class='p13n-sc-price']/text()").extract()[0]
            except:
                item['price'] = '没有价格'
            item['picture'] = li.xpath("./span[@class='a-list-item']//img/@src").extract()[0]
            print(response.request.headers)
            yield item
        try:
        # 获取下一页跳转
            nextpage = response.xpath("//div[@class='a-row a-spacing-top-mini']/div/ul/li[@class='a-last']/a/@href").extract()[0]
            print(nextpage)
            yield scrapy.Request(url=nextpage,callback=self.parse)
        except:
            pass
